package com.app.controller;

import com.app.mapper.ConfigMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.StringTool;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@Controller
@RestController
public class OpinionController {

    @Autowired
    UserService userService;
    @Autowired
    OpinionService opinionService;
    @Autowired
    OpinionImgService opinionImgService;
    @Autowired
    private ConfigMapper configMapper;
    @PostMapping("opinionAdd")
    public Result<Object> opinionAdd(
            @RequestParam("content") String content,
            @RequestParam("type") Integer type,
            @RequestParam(required = false) String imgUrls,
            HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String myMobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(myMobile);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        OpinionModel opinionModel = new OpinionModel();
        opinionModel.setType(type);
        opinionModel.setUserId(user.getId());
        opinionModel.setUserType(0);
        opinionModel.setContent(content);
        opinionService.insert(opinionModel);
        if(StringTool.isNotEmpty(imgUrls))
        {
            String[] strs = imgUrls.split(",");
            for (int i = 0; i < strs.length; i++) {
                OpinionImgModel opinionImgModel = new OpinionImgModel();
                opinionImgModel.setImgUrls(strs[i]);
                opinionImgModel.setOpinionId(opinionModel.getId());
                opinionImgService.insert(opinionImgModel);
            }
        }
        return Result.success("操作成功");
    }
    //意见反馈类别列表
    @GetMapping("/typeList")
    public Result<Object> typeList() {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", 8);
        return Result.success(configMapper.selectList(queryWrapper));
    }
}
